#include <iterator>
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <cctype>
#include <cstdio>
#include "lib/functions.h"
#include <set>
using namespace std;

void PrintMapToFile(char *, const map<string, int> &);

struct SetCompare
{
    bool operator()(const pair<string, int>& firstPair, const pair<string, int>& secondPair) const
    {
        return firstPair.second >= secondPair.second;
    }
};

int main(int argc, char * argv[])
{
    map<string,int> wordFreq;
	GetMap(argv[1], wordFreq);
    PrintMapToFile((char *)"outfreqcount1.txt", wordFreq);
}

void PrintMapToFile(char * fileName, const map<string, int> &hmap)
{
	map<string, int> freqMap = hmap;
    ofstream fout;
    fout.open(fileName, ios::out | ios::app );

	std::set<pair<string,int>, SetCompare > freqSet;
    for(map<string, int>::const_iterator mapIter = freqMap.begin(); mapIter != freqMap.end(); ++mapIter)
    {
        freqSet.insert(*mapIter);
    }

    for(set<pair<string, int> >::const_iterator setIter = freqSet.begin(); setIter != freqSet.end(); ++setIter)
    {
        fout<<setIter->first<<"\t"<<setIter->second<<endl;
    }

}
